package com.dp.appi.dao.impl;

import com.dp.appi.dao.SysConfigDao;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by chenghao on 2015/9/30.
 */
@Component
public class SysConfigDaoImpl extends BaseDaoImpl implements SysConfigDao {
    @Override
    public List<Map<String, Object>> findBaseConfig() {
        String sql = "select config_id as `key`, value from t_s_sys_config where flag=1";
        return findLists(sql);
    }

    @Override
    public List<Map<String, Object>> findPayment() {
        String sql = "select id, title, logo_path from t_s_payment where enable=1 ";
        return findLists(sql);
    }

    /**
     * 获取系统码表
     *
     * @param key 主键id
     * @return
     */
    @Override
    public List<Map<String, Object>> sysType(String key) {
        StringBuilder sb = new StringBuilder();
        sb.append("select typecode,typename");
        sb.append(" from t_s_type tst");
        sb.append(" INNER JOIN t_s_typegroup tstg on tst.typegroupid=tstg.ID");
        sb.append(" where tstg.ID=? order by typecode ");
        return getJdbcTemplate().queryForList(sb.toString(), new Object[]{key});
    }

    /**
     * 获取系统配置标志
     *
     * @param id 主键
     * @return
     */
    @Override
    public String getSysConfigById(Integer id) {
        String sql = "select * from t_s_sys_config where config_id=?";
        Map<String, Object> c = findOneObject(sql, new Object[]{id});
        if (c != null && c.get("value") != null) {
            return String.valueOf(c.get("value"));
        }
        return null;
    }

    @Override
    public Map<String, Object> findSysArticleContent(Map<String, String> param) {
        String id = param.get("id"); //文章id

        String sql = "select t1.id,t1.title,t1.subtitle,t1.contents,UNIX_TIMESTAMP(t1.publish_time) as publish_time " +
                "from t_article as t1 where t1.id=?";

        return findOneObject(sql, new Object[]{id});
    }

    @Override
    public Map<String, Object> findSysConfig(Integer id) {
        String sql = "select * from t_s_sys_config where config_id=?";
        return findOneObject(sql, new Object[]{id});
    }

    /**
     * 根据代码获取广告
     *
     * @param flag
     * @return
     */
    @Override
    public List<Map<String, Object>> getHomeAdvert(String flag) {
        StringBuilder sb = new StringBuilder("");
        sb.append(" select");
        sb.append(" tai.id,");
        sb.append(" tai.name,");
        sb.append(" tai.pic,");
        sb.append(" tai.url");
        sb.append(" from t_ad_info tai");
        sb.append(" left join t_ad_position tap on tai.position_id=tap.id");
        sb.append(" where tai.enable = 1 and tap.code = ?");
        sb.append(" order by tai.show_index asc");
        return getJdbcTemplate().queryForList(sb.toString(), new Object[]{flag});
    }

    /**
     * 离线地图配置
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> offlineMap() {
        String sql = "select map_id,province,city,map_type,size,url,UNIX_TIMESTAMP(update_time) update_time from b_offline_map";
        return getJdbcTemplate().queryForList(sql);
    }

    /**
     * 获取系统消息列表
     *
     * @param id
     */
    @Override
    public List<Map<String, Object>> sysMessList(String id, int s, int n) {
        StringBuffer sql = new StringBuffer();
        sql.append("select `id`, `ticker`, `title`, `content`, `object_type`, ");
        sql.append("UNIX_TIMESTAMP(`create_time`) create_time,object_id from t_push_records ");
        sql.append("where receiver_ids like ? ");
        sql.append(" ORDER BY create_time desc");
        sql.append(" LIMIT ?,? ");
        return getJdbcTemplate().queryForList(sql.toString(), "%" + id + "%", s, n);
    }

    /**
     * 根据user_name获取userMap
     *
     * @param user_name
     */
    @Override
    public Map<String, Object> getUserByOpenIdent(String user_name) {
        String sql = "select a.*,b.org_id from t_s_base_user a left join t_s_user_org b on a.ID = b.user_id where username= ?";
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql, user_name);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取系统当前最新版本
     *
     * @param soft_type 安卓11 IOS12
     * @return
     */
    @Override
    public Map<String, Object> getSysVersion(Integer soft_type) {

        String sql = "select app_id,DATE_FORMAT(release_time ,'%Y年%m月%d日 %H:%i') as release_time,content,version,master_version,download_addr,upgrade " +
                "from t_s_version where isable = 1 and type = 1002 and soft_type = ? " +
                "order by version+0 desc,release_time desc limit 1";

        List<Map<String, Object>> dataList = getJdbcTemplate().query(sql, new Object[]{soft_type}, getColumnMapRowMapper());

        if (dataList != null && dataList.size() > 0) {
            return dataList.get(0);
        }
        return null;
    }

}
